﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;
using System.Web.Core;
using System.Collections.Specialized;

namespace Yuanlin.Business
{
    public class AquacultureAddControl : YuanlinUserControl
    {
        protected Literal ltlTile;
        protected ValuedTextBox titleValuedTextBox;//常用名
        protected ValuedTextBox aliasValuedTextBox;//别名
        protected FileUpload attachmentData;//图片
        protected ValuedTextBox latinNameValuedTextBox;//拉丁名
        protected ValuedDropDownList categoryValuedDropDownList;//种类
        protected CheckBoxList appCategoryCheckBoxList;//应用分类
        private int categoryType = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryType);
        private int categoryId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryId);
        private int fisheryFeedbackId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.AquacultureFeedbackId);

        protected IButton SaveButton;
        protected IButton BackButton;

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack && !AjaxManager.IsCallBack)
            {
                Bind();
            }
            SaveButton.Click += new EventHandler(SaveButton_Click);
            BackButton.Click += new EventHandler(BackButton_Click);
        }

        public void BackButton_Click(object sender, EventArgs e)
        {
            Page.Response.Redirect(SiteUrls.Instance().GetAquacultureListUrl(string.Empty, string.Empty, categoryType));
        }
        public void SaveButton_Click(object sender, EventArgs e)
        {
            Aquaculture aquaculture = new Aquaculture();

            aquaculture.Title.Value = CensorWordManager.CensorWord(titleValuedTextBox.Value);
            aquaculture.Alias.Value = CensorWordManager.CensorWord(aliasValuedTextBox.Value);
            string queryValue = "";
            if (aquaculture.Alias.Value.Length == 0)
            {
                queryValue = string.Format("、{0}", aquaculture.Title.Value);
            }
            else
            {
                queryValue = string.Format("、{0}、{1}", aquaculture.Title.Value, aquaculture.Alias.Value);
            }
            aquaculture.QueryValue.Value = queryValue;
            aquaculture.LatinName.Value = CensorWordManager.CensorWord(latinNameValuedTextBox.Value);
            aquaculture.CategoryId.Value = Int32.Parse(categoryValuedDropDownList.Value);
            aquaculture.AttachmentFileName.Value = SiteUrls.Instance().GetPostedFileName(attachmentData);
            aquaculture.CategoryType.Value = categoryType;

            if(BusinessManager.CreateAquaculture(aquaculture) ==-2)
            {
                Globals.ShowMessage(this.Page, "添加失败", "此信息已经存在！");
                return;
            }

            if (SiteUrls.Instance().IsAttachmentValid(attachmentData))
            {
                SiteUrls.Instance().SaveFile(SiteUrls.Instance().GetFile(aquaculture), attachmentData);
            }

            CreateDefaultSection(aquaculture.EntityId.Value);

            //创建水产与应用分类的关系
            AquacultureAndAppCategoryRelation relation = new AquacultureAndAppCategoryRelation();
            relation.EntityId.Value = 1;
            relation.AquacultureId.Value = aquaculture.EntityId.Value;
            relation.CategoryType.Value = categoryType;
            foreach (ListItem item in this.appCategoryCheckBoxList.Items)
            {
                if (item.Selected)
                {
                    relation.CategoryIds += "," + Globals.ChangeType<int>(item.Value);
                }
            }
            if (!string.IsNullOrEmpty(relation.CategoryIds))
            {
                relation.CategoryIds = relation.CategoryIds.Substring(1);
                BusinessManager.CreateAquacultureAndAppCategoryRelation(relation);
            }

            Page.Response.Redirect(SiteUrls.Instance().GetAquacultureListUrl(string.Empty, string.Empty, categoryType));
        }

        private void Bind()
        {
            SetLtlTitle();

            //植物分类
            AquacultureCategoryListRequest request = new AquacultureCategoryListRequest();
            request.PageIndex = 0;
            request.PageSize = Int32.MaxValue;
            EntityList categories = BusinessManager.GetAquacultureCategoryList(RequestHelper.BuildGetAquacultureCategoryListRequest(request));

            AquacultureCategory topCategory = new AquacultureCategory();
            topCategory.Title.Value = "<选择植物分类>";
            topCategory.EntityId.Value = -1;
            categories.Insert(0, topCategory);

            this.categoryValuedDropDownList.DataSource = categories;
            this.categoryValuedDropDownList.DataTextField = "Title";
            this.categoryValuedDropDownList.DataValueField = "EntityId";
            this.categoryValuedDropDownList.DataBind();

            //应用分类
            AquacultureAppCategoryListRequest request1 = new AquacultureAppCategoryListRequest();
            request1.PageIndex = 0;
            request1.PageSize = Int32.MaxValue;
            EntityList appCategories = BusinessManager.GetAquacultureAppCategoryList(RequestHelper.BuildGetAquacultureAppCategoryListRequest(request1));

            this.appCategoryCheckBoxList.DataSource = appCategories;
            this.appCategoryCheckBoxList.DataTextField = "Title";
            this.appCategoryCheckBoxList.DataValueField = "EntityId";
            this.appCategoryCheckBoxList.DataBind();

            if (fisheryFeedbackId > 0)
            {
                titleValuedTextBox.Text = BusinessManager.GetAquacultureFeedback(fisheryFeedbackId).Title.Value;
                titleValuedTextBox.Enabled = false;
            }
        }
        private void SetLtlTitle()
        {
            string str = "";
            switch (categoryType)
            {
                case (int)CategoryType.Fishery:
                    {
                        str = ResourceManager.GetString("System_Applications_Yuanlin_AddFishery", "ControlPanelResources.xml");
                    }
                    break;
                case (int)CategoryType.Livestock:
                    {
                        str = ResourceManager.GetString("System_Applications_Yuanlin_AddLivestock", "ControlPanelResources.xml");
                    }
                    break;
            }
            ltlTile.Text = str;
        }
        private void CreateDataToDB(Sections sections, int aquacultureId, int sectionId)
        {
            AquacultureSection_Section aquacultureSection_Section = null;
            foreach (Section section in sections.Section)
            {
                aquacultureSection_Section = new AquacultureSection_Section();
                aquacultureSection_Section.Subject.Value = section.Name;
                aquacultureSection_Section.Enabled.Value = 1;
                aquacultureSection_Section.AquacultureId.Value = aquacultureId;
                aquacultureSection_Section.CategoryType.Value = categoryType;
                aquacultureSection_Section.CategoryId.Value = sectionId;
                BusinessManager.CreateAquacultureSection_Section(aquacultureSection_Section);
            }
        }
        private void CreateDefaultSection(int aquacultureId)
        {
            string str = "Aquaculture";
            switch (categoryType)
            {
                case (int)CategoryType.Fishery:
                    {
                        str = "Aquaculture";
                    }
                    break;
                case (int)CategoryType.Livestock:
                    {
                        str = "Livestock";
                    }
                    break;
            }
            DefaultSection defaultSection = DefaultSectionManager.GetDefaultSection(str);
            if (defaultSection == null)
            {
                return;
            }
            Sections sections = defaultSection.Sections["1"] as Sections;
            if (sections != null)
            {
                CreateDataToDB(sections, aquacultureId,1);
            }

            sections = defaultSection.Sections["2"] as Sections;
            if (sections != null)
            {
                CreateDataToDB(sections, aquacultureId,2);
            }

            sections = defaultSection.Sections["3"] as Sections;
            if (sections != null)
            {
                CreateDataToDB(sections, aquacultureId,3);
            }

            sections = defaultSection.Sections["4"] as Sections;
            if (sections != null)
            {
                CreateDataToDB(sections, aquacultureId,4);
            }

            sections = defaultSection.Sections["5"] as Sections;
            if (sections != null)
            {
                CreateDataToDB(sections, aquacultureId,5);
            }

            sections = defaultSection.Sections["6"] as Sections;
            if (sections != null)
            {
                CreateDataToDB(sections, aquacultureId,6);
            }
            sections = defaultSection.Sections["7"] as Sections;
            if (sections != null)
            {
                CreateDataToDB(sections, aquacultureId,7);
            }
        }
    }
}

